<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Example: Using ImageLoader with CSS Class Names</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <script src="../../build/yui/yui-min.js"></script>
</head>
<body>

<div id="doc">
    <h1>Example: Using ImageLoader with CSS Class Names</h1>

    

    <div class="yui3-g">
        <div id="main" class="yui3-u">
            <div class="content"><div class="intro">

  <p>
    The <a href="http://developer.yahoo.com/yui/imageloader/">ImageLoader Utility</a> allows you an alternate method of 
    using CSS class names to load images.
  </p>

  <p>
    Hover over each image to show its triggers and limit. Try tripping the triggers to see the load reactions. 
    Refresh the page to reset the images.
  </p>

</div>

<div class="example">
  <!--BEGIN SOURCE CODE FOR EXAMPLE =============================== -->

  <style>

  .everything { position:relative; height:420px; }
  .everything div { border:1px solid #888; }
  .topmain { position:absolute; top:10px; left:120px; height:75px; width:100px; }
  .duo1 { position:absolute; top:130px; left:20px; height:67px; width:100px; }
  .duo2 { position:absolute; top:130px; left:220px; height:53px; width:100px; }
  .scroll { position:absolute; top:320px; left:120px; height:72px; width:100px; }

  .yui3-imgload-maingroup,
  .yui3-imgload-duogroup,
  .yui3-imgload-scrollgroup { background:none !important; }

  </style>


  <div class='everything' id='everything'>
    <div class='topmain yui3-imgload-maingroup' id='topmain' style='background-image:url("http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/museum.jpg");' title='group 1; mouseover image; 2 sec limit'></div>
    <div class='duo1 yui3-imgload-duogroup' id='duo1' style='background-image:url("http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/uluru.jpg");' title='group 2; mouseover left image, or click on right image; 4 sec limit'></div>
    <div class='duo2 yui3-imgload-duogroup' id='duo2' style='background-image:url("http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/katatjuta.jpg");' title='group 2; mouseover left image, or click on right image; 4 sec limit'></div>
    <div class='scroll' title='group 3; scroll; no time limit'>
      <img id='scrollImg' class='yui3-imgload-scrollgroup' style='background-image:url("http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/morraine.jpg");' src='http://l.yimg.com/a/i/us/tr/b/1px_trans.gif' width='100' height='72' />
    </div>
  </div>

  <script>

    YUI({filter:"debug", logInclude: {"imageloader":true, "example":true}}).use("imageloader", function(Y) {

    	var mainGroup = new Y.ImgLoadGroup({ name: 'group 1', timeLimit: 2, className: 'yui3-imgload-maingroup' });
    	mainGroup.addTrigger('#topmain', 'mouseover');

    	var duoGroup = new Y.ImgLoadGroup({ name: 'group 2', timeLimit: 4, className: 'yui3-imgload-duogroup' });
    	duoGroup.addTrigger('#duo1', 'mouseover').addTrigger('#duo2', 'click');

    	var scrollGroup = new Y.ImgLoadGroup({ name: 'group 3', className: 'yui3-imgload-scrollgroup' });
    	scrollGroup.addTrigger(window, 'scroll');

    });

  </script>

  <!--END SOURCE CODE FOR EXAMPLE =============================== -->
</div>

<h2>Using CSS Class Names to Load Images</h2>

<p>
  Instead of registering specific image ids/URLs with a group, you can simply tag the group with a CSS class. 
  The group will later use this class name to identify which DOM elements belong to the group. 
  Each group should have one corresponding class. Each class must have a <code>background:none</code> CSS definition 
  at the top of the page, as in this example:
  <h5>CSS</h5>
<pre class="code prettyprint">.yui3-imgload-maingroup,
.yui3-imgload-duogroup,
.yui3-imgload-scrollgroup{ 
  background:none !important; 
}</pre>

  <h5>HTML</h5>
<pre class="code prettyprint">&lt;div class=&#x27;topmain yui-imgload-maingroup&#x27; id=&#x27;topmain&#x27; style=&#x27;background-image:url(&quot;http:&#x2F;&#x2F;developer.yahoo.com&#x2F;yui&#x2F;docs&#x2F;assets&#x2F;examples&#x2F;exampleimages&#x2F;small&#x2F;museum.jpg&quot;);&#x27;&gt;&lt;&#x2F;div&gt;
&lt;div class=&#x27;duo1 yui-imgload-duogroup&#x27; id=&#x27;duo1&#x27; style=&#x27;background-image:url(&quot;http:&#x2F;&#x2F;developer.yahoo.com&#x2F;yui&#x2F;docs&#x2F;assets&#x2F;examples&#x2F;exampleimages&#x2F;small&#x2F;uluru.jpg&quot;);&#x27;&gt;&lt;&#x2F;div&gt;
&lt;div class=&#x27;duo2 yui-imgload-duogroup&#x27; id=&#x27;duo2&#x27; style=&#x27;background-image:url(&quot;http:&#x2F;&#x2F;developer.yahoo.com&#x2F;yui&#x2F;docs&#x2F;assets&#x2F;examples&#x2F;exampleimages&#x2F;small&#x2F;katatjuta.jpg&quot;);&#x27;&gt;&lt;&#x2F;div&gt;
&lt;div class=&#x27;scroll&#x27;&gt;
  &lt;img id=&#x27;scrollImg&#x27; class=&#x27;yui-imgload-scrollgroup&#x27; style=&#x27;background-image:url(&quot;http:&#x2F;&#x2F;developer.yahoo.com&#x2F;yui&#x2F;docs&#x2F;assets&#x2F;examples&#x2F;exampleimages&#x2F;small&#x2F;morraine.jpg&quot;);&#x27; src=&#x27;http:&#x2F;&#x2F;l.yimg.com&#x2F;a&#x2F;i&#x2F;us&#x2F;tr&#x2F;b&#x2F;1px_trans.gif&#x27; width=&#x27;100&#x27; height=&#x27;72&#x27; &#x2F;&gt;
&lt;&#x2F;div&gt;</pre>

</p>

<p>
  A few things to note. First, the images have class names matching those in the style definitions above.
</p>

<p>
  Second, the image URL is set in the <code>background-image</code> of the elements. The <code>background:none</code> 
  defined earlier in the CSS will be removed by the ImageLoader Utility JavaScript when the images are eventually loaded.
</p>

<p>
  Third, since the <code>&lt;img&gt;</code> element displays its images through the <code>background-image</code>, 
  its size won't change when the image is loaded. Therefore the <code>width</code>/<code>height</code> needs to be set in the HTML. 
  And since that gives the image a substantial size, the browsers would show a missing-image icon if the <code>src</code> 
  attribute were not specified. Therefore we need to set one; a transparent one so that the background image will show through.
</p>

<p>
  This brings up an important limitation with this approach: you cannot alter the natural size of the image. 
  Because the image is displayed as a background image, the browser will not resize the image according to the 
  <code>width</code>/<code>height</code> of the <code>&lt;img&gt;</code> element.
</p>

<p>
  Now let's turn to the JavaScript. Since the image URLs are already specified in the HTML, we don't need them in the JS. 
  All each group needs to know is the CSS class name that will identify the images.
</p>

<pre class="code prettyprint">var mainGroup = new Y.ImgLoadGroup({ name: &#x27;group 1&#x27;, timeLimit: 2, className: &#x27;yui-imgload-maingroup&#x27; });
mainGroup.addTrigger(&#x27;#topmain&#x27;, &#x27;mouseover&#x27;);
 
var duoGroup = new Y.ImgLoadGroup({ name: &#x27;group 2&#x27;, timeLimit: 4, className: &#x27;yui-imgload-duogroup&#x27; });
duoGroup.addTrigger(&#x27;#duo1&#x27;, &#x27;mouseover&#x27;).addTrigger(&#x27;#duo2&#x27;, &#x27;click&#x27;);
 
var scrollGroup = new Y.ImgLoadGroup({ name: &#x27;group 3&#x27;, className: &#x27;yui-imgload-scrollgroup&#x27; });
scrollGroup.addTrigger(window, &#x27;scroll&#x27;);</pre>


<p>
  Note that you are free to combine this class-name approach with the other. 
  The same group can have some images identified by class name and others by registering ids/URLs.
</p></div>
        </div>

        <div id="sidebar" class="yui3-u">
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                                    <li data-description="Demonstrates the basic features and operation of the ImageLoader Utility, deferring the loading of images until specific events happen or specific time limits expire.">
                                        <a href="basic-features.html">Basic Features of the ImageLoader Utility</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Loading images above the fold immediately while deferring the loading of images below the fold.">
                                        <a href="below-fold.html">Loading Images Below the Fold</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Using CSS class names to target specific images for deferred loading.">
                                        <a href="imageloader-class-names.html">Using ImageLoader with CSS Class Names</a>
                                    </li>
                                
                            
                        </ul>
                    </div>
                </div>
            

            
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

</body>
</html>
